package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.JiSuDaiUtil;
import com.block.datapush.service.apipush.utils.ZaoZhuangUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 即速贷
 */
@Service
public class JiSuDaiApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;


    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();

//        if(!StrUtil.equals(String.valueOf(memberSourceInfo.getChannelCode()), "86L1GU")||!StrUtil.equals(String.valueOf(memberSourceInfo.getChannelCode()), "UNQDKX")){
//            pushApiResult.setCode("999");
//            pushApiResult.setMessage("渠道不符合要求，不进行推送");
//            return pushApiResult;
//        }


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("channelId", pushApiKeyParam.getApiCode());
        paramMap.put("phoneMd5", memberSourceInfo.getMobileMd5());


        log.info("【即速贷】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【即速贷】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        log.info("撞库结果resultObject : {}", resultObject);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "|");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "200")) {
            pushApiResult.setPushSuccess(true);
        }
        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        return null;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);

        String requestUrl = pushApiKeyParam.getRegisterUrl();

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("createName", memberSourceInfo.getRealname());
        bizMap.put("createSex", memberSourceInfo.getSex());
        bizMap.put("createPhone", memberSourceInfo.getMobile());
        bizMap.put("createcity", memberSourceInfo.getProvince() + "/" + memberSourceInfo.getCity());
        bizMap.put("idCard", memberSourceInfo.getIdcard());
        bizMap.put("userAge", memberSourceInfo.getAge());
        bizMap.put("education", JiSuDaiUtil.getxueli(memberSourceInfo.getXueli()));
        bizMap.put("house", JiSuDaiUtil.getHouse(memberSourceInfo.getHouse()));
        bizMap.put("car", JiSuDaiUtil.getCar(memberSourceInfo.getCar()));
        bizMap.put("creditrecord", JiSuDaiUtil.getyuqi(memberSourceInfo.getYuqi()));
        bizMap.put("creditlimit", JiSuDaiUtil.getxinyongka(memberSourceInfo.getXinyongka()));
        bizMap.put("lifeinsurancepolicy", JiSuDaiUtil.getbaodan(memberSourceInfo.getBaoxian()));
        bizMap.put("particle", 0);
        bizMap.put("flatType", JiSuDaiUtil.getjob(memberSourceInfo.getGongzuo()));
        bizMap.put("monthlyincome", JiSuDaiUtil.getShouRu(memberSourceInfo.getShouru()));
        bizMap.put("afauth", JiSuDaiUtil.getGjj(memberSourceInfo.getGjj()));
        bizMap.put("sesame", JiSuDaiUtil.getZhimaScore(memberSourceInfo.getZhima()));
        bizMap.put("wageform", 0);
        bizMap.put("socialsecurity", JiSuDaiUtil.getSocialInsurance(memberSourceInfo.getShebao()));
        bizMap.put("numberofloanperiods", JiSuDaiUtil.getloantime(memberSourceInfo.getLoanTime()));
        bizMap.put("appliedQuota", JiSuDaiUtil.getloan(memberSourceInfo.getLoanAmount()));
        bizMap.put("remarks", "");
        String data = JiSuDaiUtil.encrypt(JSON.toJSONString(bizMap), pushApiKeyParam.getApiKey());

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("channelId", pushApiKeyParam.getApiCode());
        paramMap.put("data", data);

        log.info("【即速贷】导单开始【queryUrl】{}【paramMap】{}【bizMap】{}", requestUrl, JSON.toJSONString(paramMap),JSON.toJSONString(bizMap));
        String result = HttpUtil.createPost(requestUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【即速贷】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "|");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "200")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }


    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }

    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        ProtocolParamResult paramResult = new ProtocolParamResult();
        return paramResult;
    }
}
